package com.shinhye.elephantsafe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shinhye.elephantsafe.utils.CreatePSWUtils;
import com.shinhye.elephantsafe.utils.CreatePSWUtils.PswBtn;

public class HomeActivity extends Activity {
	private GridView gv_funclist;
	
	private String[] func_names = {
			"手机防盗","通讯卫士","软件管理",
			"进程管理","流量统计","手机杀毒",
			"缓存清理","高级工具","设置中心"
			};
	private int[] func_images = {
			R.drawable.safe,R.drawable.callmsgsafe,R.drawable.app,
			R.drawable.taskmanager,R.drawable.netmanager,R.drawable.trojan,
			R.drawable.sysoptimize,R.drawable.atools,R.drawable.settings
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		gv_funclist = (GridView) findViewById(R.id.gv_funclist);
		
		//1. 使用SimpleAdpter
//		ArrayList<Map<String, Object>> func_list = new ArrayList<Map<String,Object>>();
//		for (int i = 0; i < func_names.length; i++) {
//			HashMap<String,Object> map = new HashMap<String, Object>();
//			map.put("name", func_names[i]);
//			map.put("image", func_images[i]);
//			func_list.add(map);
//		}
//		SimpleAdapter adpter = new SimpleAdapter(this, func_list, R.layout.item_funclist, new String[]{"name", "image"}, new int[]{R.id.tv_item, R.id.iv_item});
//		gv_funclist.setAdapter(adpter);
		
		//2. 自定义Adpter
		gv_funclist.setAdapter(new MyAdpter());
		gv_funclist.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch(position) {
				case 0:
					CreatePSWUtils.showPSWDialog(HomeActivity.this, true, new PswBtn() {
						@Override
						public void onCanceledBtn() {}
						@Override
						public void onOkBtn() {
							Intent intent = new Intent(HomeActivity.this, GuardThrefActivity.class);
							startActivity(intent);
						}
					});
					break;
				
				case 1:
					Intent intent1 = new Intent(HomeActivity.this, TelephonySafeActivity.class);
					startActivity(intent1);
					break;
					
				case 2:
					Intent intent2 = new Intent(HomeActivity.this, AppManagerActivity.class);
					startActivity(intent2);
					break;
					
				case 3:
					Intent intent3 = new Intent(HomeActivity.this, ProcessManagerActivity.class);
					startActivity(intent3);
					break;
					
				case 4:
					Intent intent4 = new Intent(HomeActivity.this, TrafficManagerActivity.class);
					startActivity(intent4);
					break;
					
				case 5:
					Intent intent5 = new Intent(HomeActivity.this, AntiVirusActivity.class);
					startActivity(intent5);
					break;
					
				case 6:
					Intent intent6 = new Intent(HomeActivity.this, CleanActivity.class);
					startActivity(intent6);
					break;
				
				case 7:
					Intent intent7 = new Intent(HomeActivity.this, AdvanceToolActivity.class);
					startActivity(intent7);
					break;
					
				case 8:
					Intent intent8 = new Intent(HomeActivity.this, SettingActivity.class);
					startActivity(intent8);
					break;
				}
			}
		});
	}
	

	/**
	 * Define a adpter to provider data to the GridView in the home activity.
	 * @author ShinHye
	 *
	 */
	class MyAdpter extends BaseAdapter {
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			if(convertView == null) {
				view = View.inflate(HomeActivity.this, R.layout.item_funclist, null);
			} else {
				view = convertView;
			}
			ImageView iv = (ImageView) view.findViewById(R.id.iv_item);
			TextView tv = (TextView) view.findViewById(R.id.tv_item);
			iv.setImageResource(func_images[position]);
			tv.setText(func_names[position]);
			
			return view;
		}

		@Override
		public int getCount() {
			return func_names.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
	}
	
	/**
	 * Delete notifications in the home activity.
	 */
	public void delete_notify(View v) {
		LinearLayout ll_notify = (LinearLayout) findViewById(R.id.ll_notify);
//		ll_notify.removeAllViews();
		ll_notify.setVisibility(View.INVISIBLE);
	}
}
